package priv.lirenhe.dissertation.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import priv.lirenhe.dissertation.dao.mysql.impl.WebUrlMapperImpl;
import priv.lirenhe.dissertation.domain.mysql.WebUrl;
import priv.lirenhe.dissertation.domain.mysql.WebUrlExample;
import priv.lirenhe.dissertation.service.WebUrlService;
import priv.lirenhe.dissertation.utils.TimeUtil;

@Service("WebUrlServiceImpl")
public class WebUrlServiceImpl implements WebUrlService{
	
	@Autowired
	@Qualifier("WebUrlMapperImpl")
	WebUrlMapperImpl webUrlMapperImpl = null;
	
	
	@Override
	public long getCountOfRealUrl() {
		WebUrlExample webUrlExampl = new WebUrlExample();
		return webUrlMapperImpl.countByExample(webUrlExampl);
	}

	@Override
	public int deleteUrlByExample(WebUrlExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteUrlByPrimaryKey(Long webUrlId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertUrl(Map<String,Map<String,String>> map) {
		
		return 0;
	}

	@Override
	public int insertUrlSelective(Map<String,Map<String,String>> map) throws ParseException {
//		String webUrlRealUrl = null;
		Map<String, String> webUrlAllFieldOfMap = null;
		WebUrl webUrl = null;
		
		System.out.println("WebUrlServiceImpl map.size()="+map.size());
		int i = 0;
		
		for(Map.Entry<String, Map<String, String>> entry:map.entrySet()){
//			webUrlRealUrl = entry.getKey();
			webUrlAllFieldOfMap = entry.getValue();
			webUrl = new WebUrl();
			String webUrlUsenumber = null;
			
			webUrl.setWebUrlRealurl(webUrlAllFieldOfMap.get("webUrlRealUrl"));
			webUrl.setWebUrlMainfatherurl(webUrlAllFieldOfMap.get("webUrlMainfatherurl"));
			webUrl.setWebUrlFatherurl(webUrlAllFieldOfMap.get("webUrlFatherurl"));
			webUrl.setWebUrlEffective(webUrlAllFieldOfMap.get("webUrlEffective"));
			webUrl.setWebUrlHasused(webUrlAllFieldOfMap.get("webUrlHasused"));
			
			if(webUrlAllFieldOfMap.get("webUrlHasused").equals("notHasUsed")){
				webUrlUsenumber = "1";
			}else{
				webUrlUsenumber = "0";
			}
			webUrl.setWebUrlUsenumber(webUrlUsenumber);
			
			webUrl.setWebUrlIndbtimestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(TimeUtil.getNowTime()));
			
			if(webUrlAllFieldOfMap.get("webUrlHasused").equals("hasUsed")){
				webUrl.setWebUrlFirstusetimestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(webUrlAllFieldOfMap.get("webUrlFirstusetimestamp")));
				webUrl.setWebUrlLastusetimestamp(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(webUrlAllFieldOfMap.get("webUrlFirstusetimestamp")));
			}
			
			
			System.out.println("webUrlMapperImpl.insertSelective="+(++i));
			webUrlMapperImpl.insertSelective(webUrl);
			
		}
		
		return 1;
	}

	@Override
	public List<WebUrl> selectUrlByExample(WebUrlExample example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public WebUrl selectUrlByPrimaryKey(Long webUrlId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateUrlByExampleSelective(WebUrl record, WebUrlExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateUrlByExample(WebUrl record, WebUrlExample example) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateUrlByPrimaryKeySelective(WebUrl record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateUrlByPrimaryKey(WebUrl record) {
		// TODO Auto-generated method stub
		return 0;
	}

}


/*
WebUrlExample webUrlExample = new WebUrlExample();
Criteria webUrlcriteria = webUrlExample.createCriteria();
webUrlcriteria.andWebUrlRealurlEqualTo(webUrlRealUrl);
List<WebUrl> listOfWebUrl = webUrlMapperImpl.selectByExample(webUrlExample);
if(listOfWebUrl.size() == 1){
	WebUrl webUrlFromDB = listOfWebUrl.get(0);
	webUrlUsenumber = webUrlFromDB.getWebUrlUsenumber();
	webUrlUsenumber = ((new Integer(webUrlUsenumber)) + 1 ) + "";
}else{
	try {
		throw new Exception("数据库web_url中的WEB_URL_REALURL不唯一");
	} catch (Exception e) {
		e.printStackTrace();
	}
}
*/
